﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace SocketAPI.ClientTools
{
    public static class ClientCommandStringParser
    {
        public static string t1 = "";
        public static string t2 = "";
        public static string c = "";
        public static string a = "";
        public static Socket cl;
        public static Socket li;
        public static ClientSocketThread cst;
        public static void parseCommandString(ClientSocketThread clientThread, Socket client, Socket listener, string line, string tier1group, string tier2group, string command, string argument)
        {
            //Set Temporary Variables
            cst = clientThread;
            t1 = tier1group;
            t2 = tier2group;
            c = command;
            a = argument;
            cl = client;
            li = listener;
            //--Add to PlayerData Dictionary if not already in it.
            if (cst.typeString == "CLIENT")
            {
                //-Get IPAddress of Client
                string ip = ((IPEndPoint)cl.RemoteEndPoint).Address.ToString();
                string port = ((IPEndPoint)cl.RemoteEndPoint).Port.ToString();
                //-Create String
                string clientStr = ip + port;
                //-Search Dictionary
                if (RemoteObjects.remoteGlobals.RemotePlayerData.ContainsKey(clientStr) == true)
                {
                    //Its in the Dictionary, Dont Add It
                }
                else
                {
                    //Its not in the Dictionary, Add It
                    RemoteObjects.remoteGlobals.RemotePlayerData.Add(clientStr, new SocketAPI.RemoteObjects.remotePlayer(ip, (int)int.Parse(port)));
                    SpecialConsole.Text.WriteLine("Client Added to PlayerData!", ConsoleColor.Red);
                }
            }
            //Parse Command
            switch (tier1group)
            {
                case "Server":
                    parseServerGroup(tier2group, command, argument);
                    break;
                case "Client":
                    parseClientGroup(tier2group, command, argument);
                    break;
            }
            //Debug to Controller
            debugController("", "", tier1group, tier2group, command, argument);
        }
        public static void parseServerGroup(string tier2group, string command, string argument)
        {
            //-Get IPAddress of Client
            string ip = ((IPEndPoint)cl.RemoteEndPoint).Address.ToString();
            string port = ((IPEndPoint)cl.RemoteEndPoint).Port.ToString();
            //-Create String
            string clientStr = ip + port;
            switch (tier2group)
            {
                case "Messaging":
                    switch (command)
                    {
                        case "send":
                            break;
                        case "login":
                            break;
                        case "logout":
                            break;
                    }
                    break;
                case "Players":
                    switch (command)
                    {
                        case "sendXPos":
                            //-Search Dictionary
                            if (RemoteObjects.remoteGlobals.RemotePlayerData.ContainsKey(clientStr) == true)
                            {
                                //Its in the Dictionary, Reset
                                RemoteObjects.remoteGlobals.RemotePlayerData[clientStr].X = (double)double.Parse(argument);
                            }
                            break;
                        case "sendYPos":
                            //-Search Dictionary
                            if (RemoteObjects.remoteGlobals.RemotePlayerData.ContainsKey(clientStr) == true)
                            {
                                //Its in the Dictionary, Reset
                                RemoteObjects.remoteGlobals.RemotePlayerData[clientStr].Y = (double)double.Parse(argument);
                            }
                            break;
                        case "sendHealth":
                            //-Search Dictionary
                            if (RemoteObjects.remoteGlobals.RemotePlayerData.ContainsKey(clientStr) == true)
                            {
                                //Its in the Dictionary, Reset
                                RemoteObjects.remoteGlobals.RemotePlayerData[clientStr].Health = (int)int.Parse(argument);
                            }
                            break;
                        case "sendRotation":
                            //-Search Dictionary
                            if (RemoteObjects.remoteGlobals.RemotePlayerData.ContainsKey(clientStr) == true)
                            {
                                //Its in the Dictionary, Reset
                                double rot = double.Parse(argument);
                                RemoteObjects.remoteGlobals.RemotePlayerData[clientStr].Rotation = rot;
                            }
                            //Debug
                            RemoteObjects.remotePlayer player = RemoteObjects.remoteGlobals.RemotePlayerData[clientStr];
                            SpecialConsole.Text.WriteLine("[" + cst.typeString + "][?] Player Debug: X: " + player.X +
                                                          ", Y: " + player.Y +
                                                          ", Rotation: " + player.Rotation +
                                                          ", Health: " + player.Health +
                                                          ", PlayerColour: " + player.PlayerColour +
                                                          ", CurrentWeapon: " + player.PlayerColour, ConsoleColor.Yellow);
                            break;
                        case "sendCWeapon":
                            //-Search Dictionary
                            if (RemoteObjects.remoteGlobals.RemotePlayerData.ContainsKey(clientStr) == true)
                            {
                                //Its in the Dictionary, Reset
                                RemoteObjects.remoteGlobals.RemotePlayerData[clientStr].CurrentWeapon = (int)int.Parse(argument);
                            }
                            break;
                        case "sendColour":
                            //-Search Dictionary
                            if (RemoteObjects.remoteGlobals.RemotePlayerData.ContainsKey(clientStr) == true)
                            {
                                //Its in the Dictionary, Reset
                                RemoteObjects.remoteGlobals.RemotePlayerData[clientStr].PlayerColour = (int)int.Parse(argument);
                            }
                            break;
                    }
                    break;
                case "Enemies":
                    switch (command)
                    {
                        case "sendDamage":
                            break;
                    }
                    break;
            }
        }
        public static void parseClientGroup(string tier2group, string command, string argument)
        {
            switch (tier2group)
            {
                case "Moderate":
                    switch (command)
                    {
                        case "kick":
                            break;
                        case "ban":
                            break;
                        case "promote":
                            break;
                    }
                    break;
                case "Messaging":
                    switch (command)
                    {
                        case "send":
                            break;
                        case "login":
                            break;
                        case "logout":
                            break;
                    }
                    break;
                case "Priority":
                    SpecialConsole.Text.WriteLine("[" + cst.typeString + "][?] Command: " + t1 + "->" + t2 + "->" + c + "->" + a, ConsoleColor.Yellow);
                    switch (a)
                    {
                        case "GameClient":
                            cst.typeString = "CLIENT";
                            break;
                        default:
                            cst.typeString = "UNK";
                            break;
                    }
                    SpecialConsole.Text.WriteLine("[" + cst.typeString + "][?] Is Now a " + cst.typeString, ConsoleColor.Yellow);
                    break;
            }
        }
        public static void debugController(string ip, string port, string tier1group, string tier2group, string command, string argument)
        {
            if (socketAPI.SocketServerConn.controllers.Count >= 1)
            {
                //There is a Controller Connected!
                //Lets send all Server Data to it...

            }
        }
    }
}
